define(["moment"], function (moment) {
	return ["$scope", "$filter", "$stateParams", "config", "MMWService","$mdDialog",
	function ($scope, $filter, $stateParams, config, $MMWService,$mdDialog) {
		$scope.init = function(){
                        $scope.codeList = [
                            {
                                name: $filter('translate')('B0106.list.item_no'),
                                column_name: 'a.PRODUCTNO'
                            },
                            {
                                name: $filter('translate')('B0106.list.customer_no'),
                                column_name: 'a.CUSTOMERNO'
                            },
                            {
                                name: $filter('translate')('B0106.list.plan_start_date'),
                                column_name: 'a.PlanStartDate',
                                data_type: 'D'
                            },
                            {
                                name: $filter('translate')('B0106.list.erp_no'),
                                column_name: 'ERPNo'
                            }
                        ];
		};	
                 // 报工还原检查
                 function rollback_pre_check(content_pre, feedback) {
                        $MMWService.sendToServer({
                                uri: 'ServicesSTD.Module_WIP.rollback_pre_check',
                                content: content_pre,
                                success: function (data) {
                                    feedback(data);
                                },
                                error: function () {
                                    //假設後端沒更新，僅版更前端，此時回傳情境0
                                    feedback('0');
                                }
                            });
                        }
                   $scope.loadWoNoList = function () {

                        $MMWService.showQueryListPNIG({
                                form_name:"PageNo_122",
                                condition: [
                                    {
                                        name:$filter('translate')('common.queryList.wo_c'), column_name:'MONo', query_mode:'2', value:$scope.plot_no,
                                       
                                    },
                                    {
                                        name: $filter('translate')('B1901.list.column01'), column_name:'BaseLotNo', query_mode:'2', value:""       
                                    },
                                    {
                                        name:  $filter('translate')('B1901.list.column02'), column_name:'OrgLotNo', query_mode:'2', value:""       
                                    },
                                    {
                                        name: $filter('translate')('common.queryList.item_c'), column_name:'ProductNo', query_mode:'2', value:""    
                                    },
                                    {
                                        name: $filter('translate')('common.queryList.item_n'), column_name:'ProductName', query_mode:'2', value:""      
                                    }
                                ],
                                header: [
                                  
                                    {label:$filter('translate')('common.queryList.item_c'), code:'productno'},
                                    {label: $filter('translate')('B0405.list.column03'), code: 'productversion' },
                                    {label:$filter('translate')('common.queryList.item_n'), code:'productname'},               
                                    { label: $filter('translate')('B1901.list.column01'), code: 'baselotno' },
                                    { label: $filter('translate')('B1901.list.column02'), code: 'orglotno' },
                                    { label: $filter('translate')('B1301.list.column16'), code: 'baseprocessno'},
                                    { label: $filter('translate')('B1301.list.column17'), code: 'baseprocessversion'},
                                    { label:$filter('translate')('common.queryList.wo_c'), code:'mono'}
                                ],
                                setCondition:function(conditions){
                               
                                    return conditions;
                                },
                                selectCode:$scope.plot_no,
                                confirm : function(selectItem){
                                 $scope.plot_no = selectItem.baselotno;
                                   // $scope.search();
                                }
                            });
                };

                // 查询生产批号列表
               
                $scope._fn = {
                    'Execute':{
                        click: () => {
                                $scope.resumeSave();
                         }  
                    }   
                 };
                  // 获取报工还原列表数据
                 $scope.search =  function () {
                        var content  = {
                            lot_no: $scope.plot_no ? $scope.plot_no: ""                            
                        };
                        $MMWService.sendToServer({
                                uri: 'ServicesSTD.Module_WIP.rollback_info_get_integration',
                                content: content,
                                success: function (data) {
                                  if(data.rollback_info.length > 0) {
                                      data.rollback_info.forEach((item,index)=>{
                                              item['index'] = index;
                                      });
                                     
                                  }
                                  $scope.list = data.rollback_info;                            
                                }
                        });      
                 };
                // 执行报工还原保存操作
                $scope.resumeSave = function () {
                      
                      var  lot_details = $scope.list.filter(item=>item.isSelect);
                      if(lot_details.length > 0) {
                         if(lot_details[0].index == 0 &&  $scope.list[lot_details.length-1].index ==lot_details[lot_details.length-1].index){
                                   var content  = {
                                        lot_details:lot_details                                     
                                    };
                                    var msg = $filter('translate')('B1901.msg.column01'); 
                                    $scope.$root.showConfirm(msg, function (dialog) {
                                        $MMWService.sendToServer({
                                                uri: 'ServicesSTD.Module_WIP.rollback_info_process_integration',
                                                content: content,
                                                success: function (data) {                                                                       
                                                  $scope.$root.showToast(data.message);
                                                  $scope.search();                                                
                                       }
                                     });
                                        dialog.hide();                                     
                                   });                  
                         } else {
                            $scope.$root.showAlert($filter('translate')('B1901.msg.column02'));
                            return;
                         }
                      }
                } 		
	}
]});